define(["jquery","bootstrap","bbcare",
    "eva_Pub/vm/fiveHighRiskInfants/fiveHighRiskInfantsQuestionnaire.servers",
    "eva_Pub/vm/fiveHighRiskInfants/fiveHighRiskInfantsQuestionnaire.ui"
    ],function ($,bootstrap,$bb,$servers,$ui) {
    var event = {};
    /*
    * 角度正常值：
    * */
    var normalAngle = {
        "股角":{//股角：1-3月龄：40-80度；4-6月龄：70-110度
            "1-3":[40,80],
            "4-6":[70,110],
            "7-9":[100,140],
            "10-12":[130,150],
        },
        "腘窝角":{//"腘窝角":1-3月龄：80-100度；4-6月龄：90-120度
            "1-3":[80,100],
            "4-6":[90,120],
            "7-9":[110,160],
            "10-12":[150,170],
        },
        "足背屈角":{//"足背屈角":60-70度（不受月龄限制）
            "1-3":[0,30],
            "4-6":[45,60],
            "7-9":[70,90],
            "10-12":[70,90],
           /* "other":[60,70]*/
        }
    };
    /*
    * 判断是否正常：
    * */
    var isNormalAngle = function(item,angle,monthAge,normalAngle){
        var ageRange,color = "green";
        /*
        * 0.5~3.5(不包括3.5)是按照1-3月龄的标准，3.5-6.5 (不包括6.5)按照 4-6月龄的标准，依次类推；
        * */
        if(monthAge >= 0.5 && monthAge < 3.5 ){
            ageRange = "1-3";
        }else if(monthAge >= 3.5 && monthAge < 6.5){
            ageRange = "4-6";
        }else if(monthAge >= 6.5 && monthAge < 9.5){
            ageRange = "7-9";
        }else if(monthAge >= 9.5 && monthAge < 12.5){
            ageRange = "10-12";
        }else{
            ageRange = "other";
        }
        if(normalAngle[item] && normalAngle[item][ageRange]){
            var range = normalAngle[item][ageRange];
            if( angle*1 < range[0] ||  angle*1 > range[1]){
                color = "red";
            }
        }
        return color;
    };
    //弹窗确定按钮：
    var sureAngleFn = function(){
        //获取
        var params = $(this).data("params");
        var val = $.trim($("#angle").val());
        var color = "";
        if(val < 0){
            $bb.toastShow("当前不可以输入负数",1500);
            $("#angle").val('');
            return;
        }
        if(val){//判断是否正常：
            color = isNormalAngle(params.itemTitle,val,params.monthAge*1,normalAngle);
        }
        var html = '';
        if(val){
            html = params.itemTitle +'：<font>'+ val +'</font>°';
        }else{
            html = params.itemTitle +'（<font>'+ val +'</font>）度';
        }
        $(params.dom).html(html).removeClass('red orange green').addClass(color);
        $("#myModal").find("."+ params.className).modal("hide");
    };
    //问卷选项点击事件：
    var spanClickFn = function(){
        var monthAge = $(this).data("age");
        var type = $(this).attr("type");
        var color = $(this).attr("color");
        var value = $(this).find("font").html();
        if(type == "textbox"){//编辑输入类型：
            var name = $(this).attr("name"),className = "textBox",inputType = "text";
            if(name == "股角" || name == "腘窝角" || name == "足背屈角"){ inputType = "number";}
            $ui.modalInit({"id":"#myModal","itemTitle":name,"title":$(this).parents("td").attr("name"),"className":className,"type":inputType,"value":value});
            $("#myModal").find("."+className).modal("show");
            //确定：
            $("#sureAngle").data("params",{"itemTitle":name,"monthAge":monthAge,"dom":this,"className":className});
            $("#sureAngle").click(sureAngleFn);

            //取消：
            $("#cancel").click(function(){
                $("#myModal").find("."+className).modal("hide");
            });
        }else{//点选事件
            $(this).addClass(color).siblings().removeClass('red orange green');//color后台返回的colour与定义的css类一致；
        }
    };
    /*
    * 保存：
    * */
    //保存的回调：
    var saveScoreCallback = function(data,params){
        $bb.toastShow("保存成功",1500);
        var html = '';
        for(var i = 0;i<data.score.length;i++){
            var answer = data.score[i].answer;
            for(var j = 0;j < answer.length;j++){
                if(answer[j].name == "股角" || answer[j].name == "腘窝角" || answer[j].name == "足背屈角"){
                    html += answer[j].name + "：" + answer[j].answer + "°；"
                }else{
                    html += answer[j].name + "：" + answer[j].answer + "；"
                }
            }
        }
        $("." + params.className).modal("hide");
        $('textarea[name="'+params.textareaName+'"]').val(html);
    };
    var toSureSave = function(dom){
        var positionsId = [];
        $("#questionnaire table").find("li").each(function(){
            var id = $(this).attr("id");
            var answer = [];
            $(this).find("span").each(function(){
                var colour = $(this).attr("class");
                //是否具有type:
                if($(this).attr("type") == "textbox"){
                    if($(this).find("font").html()){
                        answer.push({"colour":colour,"name":$(this).attr("name"), "answer":$(this).find("font").html()});
                    }
                }else{
                    if(colour){
                        answer.push({"colour":colour,"name":$(this).attr("name"), "answer":$(this).attr("val")});
                    }
                }
            });
            positionsId.push({"id":id*1,"answer":answer});
        });
        var options = $(dom).data("params");
        var inParams = $.extend({},options);
        inParams.postionScores = positionsId;
        $servers.saveScore(inParams,saveScoreCallback,inParams);
    };
    var toSaveFn = function(){
        var flag = isDoFn();
        if(flag){
            toSureSave(this);
        }else{
            var _this = this;
            $bb.toastConfirmShow(function(){
                toSureSave(_this)
            },"","题目未做完，确定提交？");
        }
    };

    /*
     * 判断是否做：
     * */
    var isDoFn = function(){
        var flag = true;
        $("#questionnaire table").find("li").each(function(){
            $(this).find("span").each(function(){
                //是否具有type:
                if($(this).attr("type") == "textbox"){
                    if(!$(this).find("font").html()){flag = false;return}
                }else{
                    if($(this).attr("class")){
                       if( $(this).attr("val") == "未检查" ){flag = false; return;}
                    }
                }
            });
        });
        return flag;
    };

    /*
    * 重置：
    * */
    var toResetFn = function(){
        $("#questionnaire table").find("li").each(function(){
            var topic = $(this).attr("topic");
            if(topic){
                var colour = $(this).find("span").eq(0).attr("color");
                $(this).find("span").eq(0).addClass(colour).siblings().removeClass("red orange green");
            }else{
                $(this).find("span").each(function(){
                    var name = $(this).attr("name");
                    var html = name +'（<font></font>）度';
                    $(this).html(html).removeClass('red orange green');
                });
            }
        });
    };
    event.init = function(options){
        $("#questionnaire li span").data("age",options.monthAge);
        $("#questionnaire").on("click","li span",spanClickFn);
        /*
        * 保存：
        * */
        $("#toSave").data("params",options);
        $("#toSave").click(toSaveFn);
        /*
        * 重置：
        * */
        $("#toReset").click(toResetFn);
    };
    return event;
});